<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateGoodsAPIRequest;
use App\Http\Requests\API\UpdateGoodsAPIRequest;
use App\Models\Goods;
use App\Repositories\GoodsRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class GoodsController
 * @package App\Http\Controllers\API
 */

class GoodsAPIController extends AppBaseController
{
    /** @var  GoodsRepository */
    private $goodsRepository;

    public function __construct(GoodsRepository $goodsRepo)
    {
        $this->goodsRepository = $goodsRepo;
    }

    /**
     * Display a listing of the Goods.
     * GET|HEAD /goods
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->goodsRepository->pushCriteria(new RequestCriteria($request));
        $this->goodsRepository->pushCriteria(new LimitOffsetCriteria($request));
        $goods = $this->goodsRepository->all();

        return $this->sendResponse($goods->toArray(), 'Goods retrieved successfully');
    }

    /**
     * Store a newly created Goods in storage.
     * POST /goods
     *
     * @param CreateGoodsAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateGoodsAPIRequest $request)
    {
        $input = $request->all();

        $goods = $this->goodsRepository->create($input);

        return $this->sendResponse($goods->toArray(), 'Goods saved successfully');
    }

    /**
     * Display the specified Goods.
     * GET|HEAD /goods/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var Goods $goods */
        $goods = $this->goodsRepository->findWithoutFail($id);

        if (empty($goods)) {
            return $this->sendError('Goods not found');
        }

        return $this->sendResponse($goods->toArray(), 'Goods retrieved successfully');
    }

    /**
     * Update the specified Goods in storage.
     * PUT/PATCH /goods/{id}
     *
     * @param  int $id
     * @param UpdateGoodsAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateGoodsAPIRequest $request)
    {
        $input = $request->all();

        /** @var Goods $goods */
        $goods = $this->goodsRepository->findWithoutFail($id);

        if (empty($goods)) {
            return $this->sendError('Goods not found');
        }

        $goods = $this->goodsRepository->update($input, $id);

        return $this->sendResponse($goods->toArray(), 'Goods updated successfully');
    }

    /**
     * Remove the specified Goods from storage.
     * DELETE /goods/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Goods $goods */
        $goods = $this->goodsRepository->findWithoutFail($id);

        if (empty($goods)) {
            return $this->sendError('Goods not found');
        }

        $goods->delete();

        return $this->sendResponse($id, 'Goods deleted successfully');
    }
}
